import { ref } from 'vue'
import { AddOrderTypeForm } from './types'
import { moduleTypeSave } from '@/api/basic'

export default () => {
  const addOrderTypeLoading = ref(false)

  const addOrderTypeForm = ref<AddOrderTypeForm>({
    id: '',
    name: '',
    parentId: '',
    sort: 0,
    status: false,
    childData: [],
  })

  const addOrderType = async (orderModuleId: string) => {
    addOrderTypeLoading.value = true
    const res = await moduleTypeSave(addOrderTypeForm.value, orderModuleId)
    addOrderTypeLoading.value = false
    return res.data as { data: object; success: boolean; code: number; message: string }
  }

  const resetAddOrderTypeForm = () => {
    addOrderTypeForm.value = {
      id: '',
      name: '',
      parentId: '',
      sort: 0,
      status: false,
      childData: [],
    }
  }

  return {
    addOrderTypeForm,
    addOrderTypeLoading,
    addOrderType,
    resetAddOrderTypeForm,
  }
}
